package persistence;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Set;

public class Serializer {

	
	public void serialize(String filename, Set<String> emails) {
		System.out.println("serialize into " + filename);
		try {
			FileOutputStream file = new FileOutputStream(filename);
			ObjectOutputStream o = new ObjectOutputStream(file);
			o.writeObject(emails);
			o.close();
		} catch (IOException e) {
			System.err.println(e);
		}
	}

	@SuppressWarnings("unchecked")
	public Set<String> deserialize(String filename) {
		Set<String> emails = null ;
		try {
			FileInputStream file = new FileInputStream(filename);
			ObjectInputStream o = new ObjectInputStream(file);
			emails =  (Set<String>) o.readObject();
			o.close();
			for (String string : emails) {
				System.out.println(string);
			}
		} catch (IOException e) {
			System.err.println(e);
			return null;
		} catch (ClassNotFoundException e) {
			System.err.println(e);
			return null;
		}
		return emails;
	}
	
}
